'''
Created on Jan 8, 2014

Rogues is a turn-based multi-player roguelike 

@author: Trognz
'''

if __name__ == '__main__':
    pass


import pygame, sys, random
import time
from gamemodel import gamentities

from const import const
from utils import drawText

# set up pygame
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
mainClock = pygame.time.Clock()

# Setup fonts
TITLE_FONT = pygame.font.SysFont(None, 48)
DEFAULT_FONT = pygame.font.SysFont(None, 30)    


# modes = pygame.display.list_modes(16)
# print(modes)

screen = pygame.display.set_mode((const.DEFAULT_WINDOW_WIDTH, const.DEFAULT_WINDOW_HEIGHT), 0, 32)

CURRENT_WINDOW_WIDTH=screen.get_width()
CURRENT_WINDOW_HEIGHT=screen.get_height()

pygame.display.set_caption(const.GAME_TITLE + " version " + str(const.GAME_BUILD))

# Set up the screen panels
characterPane=pygame.Rect(CURRENT_WINDOW_WIDTH - const.DEFAULT_CHARACTER_PANE_WIDTH, 0, const.DEFAULT_CHARACTER_PANE_WIDTH, CURRENT_WINDOW_HEIGHT)
logPane=pygame.Rect(0, CURRENT_WINDOW_HEIGHT - const.DEFAULT_LOG_PANE_HEIGHT, CURRENT_WINDOW_WIDTH, const.DEFAULT_LOG_PANE_HEIGHT)
gamePane=pygame.Rect(0,0,CURRENT_WINDOW_WIDTH - const.DEFAULT_CHARACTER_PANE_WIDTH, CURRENT_WINDOW_HEIGHT - const.DEFAULT_LOG_PANE_HEIGHT)

playerRect=pygame.Rect(0,0,0,0)

MOVESPEED = 3

# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
game = ""
player  = ""

pygame.display.update()

def initGame():
    global shoots
    global enemies
    global score
    global game
    shoots = []
    enemies = []
    score = 0
    game = ""

def showStartScreen():
    screen.fill(const.COLOR_BLACK)
    drawText(const.GAME_TITLE, TITLE_FONT, const.COLOR_WHITE, screen, (CURRENT_WINDOW_WIDTH / 3), (CURRENT_WINDOW_HEIGHT / 3))
    drawText('Press a key to start.', DEFAULT_FONT, const.COLOR_WHITE, screen, (CURRENT_WINDOW_WIDTH / 3), (CURRENT_WINDOW_HEIGHT / 3) + 50)
    pygame.display.update()

def showGameOverScreen():
    screen.fill(const.COLOR_BLACK)    
    drawText('GAME OVER', TITLE_FONT, const.COLOR_WHITE, screen, (CURRENT_WINDOW_WIDTH / 3), (CURRENT_WINDOW_HEIGHT / 3))
    drawText('Press a key to start over.', TITLE_FONT, const.COLOR_WHITE, screen, (CURRENT_WINDOW_WIDTH / 3) - 30, (CURRENT_WINDOW_HEIGHT / 3) + 50)
    pygame.display.update()

def waitForPlayerToPressKey():
    while True:
        # Sleep to avoid processor stressing during wait...
        time.sleep(0.2)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE: # pressing escape quits
                    pygame.quit()
                    sys.exit()
                return
            if event.type == pygame.MOUSEBUTTONUP:
                return

def ControllerTick():
    
    global moveDown
    global moveUp
    global moveLeft
    global moveRight
    
    # Handle input events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            # change the keyboard variables
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                moveRight = False
                moveLeft = True
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                moveLeft = False
                moveRight = True
            if event.key == pygame.K_UP or event.key == ord('w'):
                moveDown = False
                moveUp = True
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                moveUp = False
                moveDown = True
            if event.key == pygame.K_SPACE:
                shoots.append(pygame.Rect(playerRect.x + playerRect.width, playerRect.y + playerRect.height / 2, 20, 10))
                 
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                moveLeft = False
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                moveRight = False
            if event.key == pygame.K_UP or event.key == ord('w'):
                moveUp = False
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                moveDown = False
            if event.key == ord('x'):
                playerRect.top = random.randint(0, CURRENT_WINDOW_HEIGHT - playerRect.height)
                playerRect.left = random.randint(0, CURRENT_WINDOW_WIDTH - playerRect.width)
                
    return True

def ViewTick():
    
    global moveDown
    global moveUp
    global moveLeft
    global moveRight
    global score
    global topScore
    global player
    dead = False
    
    # Draw the black background onto the surface
    screen.fill(const.COLOR_BLACK)
    
    # Draw the character pane
    pygame.draw.rect(screen, const.COLOR_BLUE ,characterPane)
    pygame.draw.rect(screen, const.COLOR_GREEN ,logPane)
    pygame.draw.rect(screen, const.COLOR_WHITE, gamePane)

    # draw the player onto the surface
    screen.blit(player.playerAvatar, playerRect)

    # move the player
    if moveDown and playerRect.bottom < CURRENT_WINDOW_HEIGHT:
        playerRect.top += MOVESPEED
    if moveUp and playerRect.top > 0:
        playerRect.top -= MOVESPEED
    if moveLeft and playerRect.left > 0:
        playerRect.left -= MOVESPEED
    if moveRight and playerRect.right < CURRENT_WINDOW_WIDTH:
        playerRect.right += MOVESPEED     
    
    # handle collision here

    # draw the window onto the screen
    pygame.display.update()
    
    if dead:
        return False
    else:
        return True
    
def main():

    global player
    
    # Overall loop
    while True:
        initGame()
        showStartScreen()
        waitForPlayerToPressKey()
        
        player = gamentities.Player("Trog")
        game = gamentities.Game(player)
        
        # run the game loop
        while True:
            # check for events
            if not ControllerTick():
                return
        
            if not ViewTick():
                showGameOverScreen()
                break
            
            mainClock.tick(const.FRAME_PER_SECOND)

# Starting point of the main menu
main()

if __name__ == '__main__':
    pass

